package com.ent.util

import com.alibaba.druid.pool.DruidDataSource
import org.jooq.Configuration
import org.jooq.SQLDialect
import org.jooq.TransactionProvider
import org.jooq.conf.Settings
import org.jooq.conf.StatementType
import org.jooq.impl.DataSourceConnectionProvider
import org.jooq.impl.DefaultConfiguration
import org.jooq.impl.DefaultTransactionProvider
import java.util.*

object DB {
    /**
     * 获取configuration
     * @return Configuration
     * @example 数据库连接方式（例中为：DriverManager常规方式，方法中为：Druid连接池连接方式）
     * val connection = java.sql.DriverManager.getConnection(config.getProperty("db.url").toString(), config.getProperty("db.username").toString(), config.getProperty("db.password").toString())
     */
    fun getInstance(config: Properties): Configuration {
        val dataSource = DruidDataSource()
        dataSource.url = config.getProperty("db.url").toString()
        dataSource.username = config.getProperty("db.username").toString()
        dataSource.password = config.getProperty("db.password").toString()
        dataSource.setFilters("stat,log4j")
        val connectionProvider = DataSourceConnectionProvider(dataSource)
        val transactionProvider: TransactionProvider = DefaultTransactionProvider(connectionProvider, false)
        val settings = Settings()
        settings.statementType = StatementType.STATIC_STATEMENT
        val configuration = DefaultConfiguration().set(connectionProvider).set(transactionProvider).set(SQLDialect.MYSQL).set(settings)
        return configuration
    }
}